<template>
    <div class='invitation'>
        <div class="container">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="search-con">
                <el-form-item label="状态" prop="key_status" style="margin-right:25px;">
                    <el-select v-model="queryParams.key_status" placeholder="请选择" clearable size="small">
                        <el-option v-for="dict in statusList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="22">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleBtn('add')" v-hasPermi="['user:post:add']">
                        新增</el-button>

                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight :height="tableList && tableList.length > 0 ? '900px' : '120px'">
                <el-table-column label="排序" align="center"  width="80">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.sort" @keyup.native="scope.row.sort=handleNum(scope.row.sort)" @blur="sortTab(scope.row)" clearable size="small" />
                    </template>
                </el-table-column>
                <el-table-column label="序号" align="center" prop="id" />
                <el-table-column label="邀请图片" align="center">
                    <template slot-scope="scope">
                        <el-image style="width: 50px; height: 50px;border:1px solid #eee;" :src="scope.row.image" :preview-src-list="[scope.row.image]">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column label="邀请文案" align="center" prop="writing" show-overflow-tooltip />
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(statusList, scope.row.status) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleBtn('edit', JSON.parse(JSON.stringify(scope.row)))"
                            v-hasPermi="['basic:history:edit']">
                            编辑
                        </el-button>
                        <el-button v-if="scope.row.status==0" size="mini" type="text" @click="handleAble(scope.row)" v-hasPermi="['user:staff:stop']">停用
                        </el-button>
                        <el-button v-if="scope.row.status==1" size="mini" type="text" @click="handleAble(scope.row)" v-hasPermi="['user:staff:start']">启用
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['basic:history:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="pageCount > 0" :pageCount="pageCount" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
            <el-dialog :title="handleShow == 'add' ? '新增' : '编辑'" visible v-if="handleShow" @close="close()" width="600px" append-to-body
                :close-on-click-modal="false">
                <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="formLoading">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="邀请文案" prop="writing">
                                <el-input v-model="form.writing" type="textarea" rows="4" placeholder="请输入邀请文案"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span="24">
                            <el-form-item label="邀请图片" prop="image">
                                <el-upload v-if="handleShow='edit'" class="avatar-uploader" :action="$uploadUrl"
                                    :show-file-list="false" :on-success="handleAvatarSuccess">
                                    <img v-if="form.image" :src="form.image" class="avatar">
                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                </el-upload>
                                <el-image v-else style="width: 100px; height: 100px;border:1px solid #eee;" :src="form.image" :preview-src-list="[form.image]">
                                </el-image>
                            </el-form-item>
                        </el-col>

                    </el-row>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="close()">取 消</el-button>
                    <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { getInviteLists, setInviteInfo,setInviteStatus,delInviteInfo, setInviteSort } from "@/api/distribution/invitation.js"
import { departStatus } from "@/api/dictionary"
import throttle from 'lodash.throttle'
export default {
    name: 'invitation',
    components: {

    },
    data() {
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                page: 1,
                limit: 20,
                key_status: '',
            },
            tableList: [],
            statusList: departStatus,

            handleShow: "",
            formLoading: false,
            form: {
                writing: null,
                image: null,
            },
            rules: {
                writing: [{ required: true, message: '请输入邀请文案', trigger: 'blur' }],
                image: [{ required: true, message: '请上传老人头像', trigger: 'change' }],
            },
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            this.loading = true;
            getInviteLists(this.queryParams).then(res => {
                this.tableList = res.data.lists;
                this.pageCount = res.data.pageCount;
                this.loading = false;
            });
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        handleBtn(type, row) {
            this.title = type == "add" ? "新增" : "编辑"
            if (row) {
                console.log(row, 11111)
                let data = {
                    invite_id: row.id,
                    writing: row.writing,
                    image: row.image
                }
                this.form = data
            }
            this.handleShow = type;
        },
        close() {
            this.form = {}
            this.handleShow = ""
        },
        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    setInviteInfo(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),

        handleAvatarSuccess(res, file) {
            this.$set(this.form, 'image', res.data[0].fileurl)
        },
        sortTab(row) {
            console.log(row, 123)
            setInviteSort({
                sort: row.sort,
                invite_id: row.id
            }).then(res => {
                if (res.code == 1) {
                    this.msgSuccess("操作成功");
                    this.getList()
                }
            })
        },

        handleAble(row) {
            let label = row.status == 0 ? '停用' : '启用';
            this.$confirm(`是否将序号为“${row.id}”${label}?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setInviteStatus({ invite_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess(`${label}成功`);
            })
        },
        handleDelete(row) {
            this.$confirm('是否确认删除序号为' + '"' + row.id + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delInviteInfo({ invite_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
    },
}

</script>
<style scoped lang='scss'>
/deep/.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 100px !important;
    height: 100px !important;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px !important;
    height: 100px !important;
    line-height: 100px !important;
    /* text-align: center;
    display: block !important; */
}

.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>